package com.ftd.controller;



import com.ftd.exception.ApiException;
import com.ftd.util.Constant;
import com.ftd.viewmodel.ApiError;
import org.apache.shiro.util.ThreadContext;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author Peter on 2016-07-11.
 */
public abstract class BaseController {

    @ExceptionHandler({ApiException.class})
    @ResponseBody
    public ApiError handlerApiException(ApiException apiException,
                                        HttpServletResponse response) {
        apiException.printStackTrace();
        ApiError apiError = new ApiError(apiException.getMessage(),
                apiException.getHttpStatus());
        response.setStatus(apiError.getStatus());
        return apiError;
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ApiError handlerException(Exception exception,
                                     HttpServletResponse response) {
        exception.printStackTrace();
        ApiError apiError = new ApiError(exception.getMessage(),
                500);
        response.setStatus(apiError.getStatus());
        return apiError;
    }

    public Integer getUserId() {
        return (Integer) ThreadContext.get(Constant.USERID);
    }
}
